/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.edu.vttu;

import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

/**
 *
 * @author Lucas Nguyen
 */
public class deleteMail {
    public static boolean deleteMail( int i){
        try {
            Properties properties = System.getProperties();
            Session session = Session.getDefaultInstance(properties);
            Store store = session.getStore("pop3");
            store.connect(ConnectionServer.mailHost, ConnectionServer.kiemTraDiaChiGmail(ConnectionServer.userName),ConnectionServer.passWord);
            Folder folder = store.getFolder("inbox");
             if (!folder.exists()) {
            System.out.println("Không có hợp thư đến...");
            System.exit(0);
                           } 
             folder.open(Folder.READ_WRITE);
        Message[] messages = folder.getMessages();
         System.out.println("Bắt đầu kết nối................"); 
              
  
            Message msg = messages[i];
            msg.setFlag(Flags.Flag.DELETED, true);
            if (msg.isSet(Flags.Flag.DELETED))
                {
           folder.close(true);
        store.close(); 
              return true;
                }
            else 
                return  false;
        } catch (Exception e) {
             e.printStackTrace();
        }
        return true;
    }
}
